﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Windows.Forms;
using System.Threading;
using System.ComponentModel;
using System.Windows;


namespace Radar
{
    public partial class MainForm : Form
    {
        Radar _radar;
        Coordinate td1 = new Coordinate(1, 400, 400, 4000, 1000, "abc");
        Coordinate td2 = new Coordinate(2, 200, 200, 4000, 1000, "abc");
        List<Coordinate> qd1 = new List<Coordinate>();
        System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();

        int speed = 40;
        public static List<FlyingItem> listTarget = new List<FlyingItem>();
        public static List<Rocket> listTenlua = new List<Rocket>();

        public MainForm()
        {
            InitializeComponent();
            t.Interval = 500;
            t.Tick += new EventHandler(t_Tick);
            t.Enabled = true;
            backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(backgroundWorker1_DoWork);

        }

        void t_Tick(object sender, EventArgs e)
        {
            double max_angle = _radar.maxAngle * 0.0174532925d;

            if (listTarget.Count == 0)
            {
                lvTarget.Items.Clear();
                cbTarget.Items.Clear();
                cbTarget.Text = "";
            }
            else
            {
                //lvTarget.Items.Clear();
                cbTarget.Items.Clear();
                // select which of the three items to update
                foreach (FlyingItem item in listTarget)
                {

                    //khoang cach di duoc cua muc tieu trong 1 s
                    double s = item._current.velocity * 1000 / (3600000 * 3);

                    //toa do tuong doi giua diem di va diem den
                    double x = item._to.X - item._current.X;
                    double y = item._to.Y - item._current.Y;
                    double chenhlech_kc = Math.Sqrt(x * x + y * y);
                    double gocngang = Math.Acos(x / chenhlech_kc);
                    //doi sang do.
                    gocngang = gocngang / 0.0174532925d;

                    if (y < 0)
                    {
                        gocngang = 360d - gocngang;
                    }
                    //doi sang radian de tinh sin va cos
                    gocngang *= 0.0174532925d;
                    double z = item._to.Z - item._current.Z;
                    double gocdung = Math.Asin(z / Math.Sqrt(z * z + chenhlech_kc * chenhlech_kc));
                    //toa do muc tieu sau 1s
                    double z1 = item._current.Z + (s * Math.Sin(gocdung));
                    double chenhlech_kc1 = (float)(s * Math.Abs(Math.Cos(gocdung)));

                    double x1 = item._current.X + chenhlech_kc1 * Math.Cos(gocngang);
                    double y1 = item._current.Y + chenhlech_kc1 * Math.Sin(gocngang);
                    Coordinate current = new Coordinate(item._current.stt, x1, y1, z1, item._current.velocity, "abc");
                    //gan diem nay thanh diem bat dau
                    item._current = current;

                    if (z >= 0)
                    {
                        item._current = current;
                        if (z1 >= item._to.Z)
                        {

                            for (int k = 0; k < item.airline.Count; k++)
                            {
                                Coordinate td = item.airline[k];
                                if (td1.stt == item._to.stt)
                                    item._to = item.airline[k + 1];
                                else
                                { }
                            }
                        }
                        else
                        {

                        }
                    }
                    else
                    {
                        item._current = current;
                        if (z1 <= item._to.Z)
                        {

                            for (int k = 0; k < item.airline.Count; k++)
                            {
                                Coordinate td = item.airline[k];
                                if (td1.stt == item._to.stt)
                                    item._to = item.airline[k + 1];
                                else
                                { }
                            }
                        }
                        else
                        {

                        }
                    }

                    _radar.AddItem(item);

                    _radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);

                    if (_radar._az + 20 > Radar.getAzimuth(current) && _radar._az - 20 < Radar.getAzimuth(current) && Radar.getRange(current) <= pictureBox1.Width / 2 && (Radar.getRange(item._current) * Math.Sin(max_angle)) > item._current.Z)
                    {


                        bool Flag = true;
                        for (int j = 0; j < lvTarget.Items.Count; j++)
                        {

                            if (lvTarget.Items[j].Text.Trim() == item.ID.ToString().Trim())
                            {
                                ListViewItem lvItem = lvTarget.Items[j];
                                lvItem.Text = item.ID.ToString();
                                lvItem.SubItems[1].Text = (item._category);
                                lvItem.SubItems[2].Text = (Radar.getAzimuth(item._current).ToString());

                                int rg = (int)Radar.getRange(item._current);

                                lvItem.SubItems[3].Text = (rg.ToString());
                                lvItem.SubItems[4].Text = (item._current.Z.ToString());
                                lvItem.SubItems[5].Text = (item._current.velocity.ToString());
                                Flag = false;
                            }
                        }
                        if (Flag)
                        {
                            ListViewItem lvItem = new ListViewItem();
                            lvItem.Text = item.ID.ToString();
                            lvItem.SubItems.Add(item._category);
                            lvItem.SubItems.Add(Radar.getAzimuth(item._current).ToString());

                            int rg = (int)Radar.getRange(item._current);

                            lvItem.SubItems.Add(rg.ToString());
                            lvItem.SubItems.Add(item._current.Z.ToString());
                            lvItem.SubItems.Add(item._current.velocity.ToString());
                            lvItem.BackColor = item._color;
                            lvTarget.Items.Add(lvItem);
                        }

                        if (Radar.getRange(current) <= 60)
                        {
                            System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer();
                            myPlayer.SoundLocation = @"c:\sound.wav";
                            myPlayer.Play();

                        }

                    }
                    cbTarget.Items.Add(item.ID);

                }
            }
            if (listTenlua.Count == 0)
            { }
            else
            {
                //lvTarget.Items.Clear();
                //cbTarget.Items.Clear();
                // select which of the three items to update
                for (int m = 0; m < listTenlua.Count; m++)
                {
                    Rocket item = listTenlua[m];
                    //khoang cach di duoc cua muc tieu trong 1 s
                    double s = item._current.velocity * 1000 / (3600000 * 3);


                    //toa do tuong doi giua diem di va diem den
                    double x = item._to.X - item._current.X;
                    double y = item._to.Y - item._current.Y;
                    double chenhlech_kc = Math.Sqrt(x * x + y * y);
                    double gocngang = Math.Acos(x / chenhlech_kc);
                    //doi sang do.
                    gocngang = gocngang / 0.0174532925d;

                    if (y < 0)
                    {
                        gocngang = 360d - gocngang;
                    }
                    //doi sang radian de tinh sin va cos
                    gocngang *= 0.0174532925d;
                    double z = item._to.Z - item._current.Z;
                    double gocdung = Math.Asin(z / Math.Sqrt(z * z + chenhlech_kc * chenhlech_kc));
                    //toa do muc tieu sau 1s
                    double z1 = item._current.Z + (s * Math.Sin(gocdung));
                    double chenhlech_kc1 = (float)(s * Math.Abs(Math.Cos(gocdung)));

                    double x1 = item._current.X + chenhlech_kc1 * Math.Cos(gocngang);
                    double y1 = item._current.Y + chenhlech_kc1 * Math.Sin(gocngang);
                    Coordinate current = new Coordinate(item._current.stt, x1, y1, z1, item._current.velocity, "abc");
                    //gan diem nay thanh diem bat dau
                    item._current = current;
                    //tim vi tri hien tai cua muc tieu
                    for (int i = 0; i < listTarget.Count; i++)
                    {
                        FlyingItem mb = listTarget[i];

                        if (mb.ID == item.targetID)
                        {
                            if ((int)mb._current.X == (int)item._current.X && (int)mb._current.Y == (int)item._current.Y)
                            {

                                for (int n = 0; n < _radar.TargetList.Count; n++)
                                {
                                    FlyingItem mb1 = _radar.TargetList[n];
                                    if (mb1.ID == item.ID) _radar.TargetList.Remove(mb1);

                                }
                                for (int n = 0; n < _radar.TargetList.Count; n++)
                                {
                                    FlyingItem mb1 = _radar.TargetList[n];
                                    if (mb1.ID == mb.ID) _radar.TargetList.Remove(mb1);
                                }
                                for (int n = 0; n < lvTarget.Items.Count; n++)
                                {
                                    if (lvTarget.Items[n].Text == mb.ID.ToString())
                                    {
                                        lvTarget.Items.Remove(lvTarget.Items[n]);

                                    }
                                    if (lvTarget.Items[n].Text == item.ID.ToString())
                                        lvTarget.Items.Remove(lvTarget.Items[n]);
                                }
                                listTenlua.Remove(item); listTarget.Remove(mb);

                                backgroundWorker1.RunWorkerAsync();
                                MessageBox.Show("Mục tiêu đã bị tiêu diệt");





                            }

                            else
                            {
                                item._to = mb._current;
                                _radar.AddItem(item);


                                if (_radar._az + 20 > Radar.getAzimuth(current) && _radar._az - 20 < Radar.getAzimuth(current) && Radar.getRange(current) <= pictureBox1.Width / 2 && (Radar.getRange(item._current) * Math.Sin(max_angle)) > item._current.Z)
                                {

                                    bool Flag = true;
                                    for (int j = 0; j < lvTarget.Items.Count; j++)
                                    {

                                        if (lvTarget.Items[j].Text.Trim() == item.ID.ToString().Trim())
                                        //lvTarget.Items.Remove(lvTarget.Items[j]);
                                        {
                                            ListViewItem lvItem = lvTarget.Items[j];
                                            lvItem.Text = item.ID.ToString();
                                            lvItem.SubItems[1].Text = (item._category);
                                            lvItem.SubItems[2].Text = (Radar.getAzimuth(item._current).ToString());

                                            double rg = Radar.getRange(item._current);

                                            lvItem.SubItems[3].Text = (rg.ToString());
                                            lvItem.SubItems[4].Text = (item._current.Z.ToString());
                                            lvItem.SubItems[5].Text = (item._current.velocity.ToString());
                                            Flag = false;
                                        }
                                    }
                                    if (Flag)
                                    {
                                        ListViewItem lvItem = new ListViewItem();
                                        lvItem.Text = item.ID.ToString();
                                        lvItem.SubItems.Add(item._category);
                                        lvItem.SubItems.Add(Radar.getAzimuth(item._current).ToString());

                                        double rg = Radar.getRange(item._current);

                                        lvItem.SubItems.Add(rg.ToString());
                                        lvItem.SubItems.Add(item._current.Z.ToString());
                                        lvItem.SubItems.Add(item._current.velocity.ToString());
                                        lvItem.BackColor = item._color;
                                        lvTarget.Items.Add(lvItem);
                                    }
                                }
                            }
                        }
                    }
                }
            }

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            System.Media.SoundPlayer myPlayer = new System.Media.SoundPlayer();
            myPlayer.SoundLocation = @"c:\sound1.wav";
            myPlayer.Play();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //Khoi tao radar voi size cua man scan = diameter, 
            //pQuetThang= chieu rong anh quet thang,
            //pQuetVong = chieu cao anh quet thang, 
            //cs_size= do dai canh cua anh quet tron va xoan oc
            _radar = new Radar(pictureBox1.Width, pQuetThang.Width, pQuetThang.Height, pQuetVong.Width);
            _radar.numberOfSpirals = 3;
            pictureBox1.Image = _radar.Image;
            _radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);
            _radar.DrawScanInterval = speed;
            txtMaxAngle.Text = _radar.maxAngle.ToString();

            trackBarSpeed.Maximum = 20;
            trackBarSpeed.TickFrequency = 1;
            pQuetThang.Image = _radar.outputImageStraightSweep;
            pQuetVong.Image = _radar.outputImageCircleSweep;
            pXoanOc.Image = _radar.outputImageSpiralSweep;

        }

        void _radar_ImageUpdate(object sender, ImageUpdateEventArgs e)
        {
            pictureBox1.Image = e.Image;
            pQuetThang.Image = _radar.outputImageStraightSweep;
            pQuetVong.Image = _radar.outputImageCircleSweep;
            pXoanOc.Image = _radar.outputImageSpiralSweep;
            txtNoSpiral.Text = _radar._az.ToString();
        }

        private void trackBarSpeed_Scroll(object sender, EventArgs e)
        {
            if (trackBarSpeed.Value == 0) trackBarSpeed.Value = 1;
            _radar.DrawScanInterval = (speed) / trackBarSpeed.Value;
            _radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);

        }

        private void rbThuan_CheckedChanged(object sender, EventArgs e)
        {
            if (rbThuan.Checked) _radar.clockwiseScan = true;
            _radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);

        }

        private void rbNguoc_CheckedChanged(object sender, EventArgs e)
        {
            if (rbNguoc.Checked) _radar.clockwiseScan = false;
            _radar.ImageUpdate += new ImageUpdateHandler(_radar_ImageUpdate);

        }

        private void cbTarget_TextChanged(object sender, EventArgs e)
        {
            if (cbTarget.Text == "")
            {
                _radar.targetCatched = 0;
            }
            else
            {
                if (_radar.targetCatched == 0)
                { }
                else
                    _radar.changeTargetFlag = true;
                int id = Convert.ToInt16(cbTarget.Text);
                _radar.targetCatched = id;

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            CreateTarget mt = new CreateTarget();
            mt.Show();
        }

        private void btTieudiet_Click(object sender, EventArgs e)
        {
            if (cbTarget.Text == "")
            { return; }
            Rocket tenlua1 = new Rocket(Convert.ToInt16(cbTarget.Text));
            listTenlua.Add(tenlua1);
        }

        private void btSwitch_Click(object sender, EventArgs e)
        {
            if (rbThang.Checked)
            {
                pQuetThang.Visible = true;
                pQuetVong.Visible = false;
                pXoanOc.Visible = false;
            }
            else if (rbTron.Checked)
            {
                pQuetThang.Visible = false;
                pQuetVong.Visible = true;
                pXoanOc.Visible = false;
            }
            else if (rbXoanOc.Checked)
            {
                pQuetThang.Visible = false;
                pQuetVong.Visible = false;
                pXoanOc.Visible = true;
            }

        }

        private void rbThang_CheckedChanged(object sender, EventArgs e)
        {
            if (rbThang.Checked)
            {
                pQuetThang.Visible = true;
                pQuetVong.Visible = false;
                pXoanOc.Visible = false;
                txtNoSpiral.Visible = false;
                label4.Visible = false;
            }
            else if (rbTron.Checked)
            {
                pQuetThang.Visible = false;
                pQuetVong.Visible = true;
                pXoanOc.Visible = false;
                txtNoSpiral.Visible = false;
                label4.Visible = false;
            }
            else if (rbXoanOc.Checked)
            {
                pQuetThang.Visible = false;
                pQuetVong.Visible = false;
                pXoanOc.Visible = true;
                txtNoSpiral.Visible = true;
                label4.Visible = true;
            }

        }

        private void rbTron_CheckedChanged(object sender, EventArgs e)
        {
            if (rbThang.Checked)
            {
                pQuetThang.Visible = true;
                pQuetVong.Visible = false;
                pXoanOc.Visible = false;
                txtNoSpiral.Visible = false;
                label4.Visible = false;
            }
            else if (rbTron.Checked)
            {
                pQuetThang.Visible = false;
                pQuetVong.Visible = true;
                pXoanOc.Visible = false;
                txtNoSpiral.Visible = false;
                label4.Visible = false;
            }
            else if (rbXoanOc.Checked)
            {
                pQuetThang.Visible = false;
                pQuetVong.Visible = false;
                pXoanOc.Visible = true;
                txtNoSpiral.Visible = true;
                label4.Visible = true;
            }

        }

        private void rbXoanOc_CheckedChanged(object sender, EventArgs e)
        {
            if (rbThang.Checked)
            {
                pQuetThang.Visible = true;
                pQuetVong.Visible = false;
                pXoanOc.Visible = false;
                txtNoSpiral.Visible = false;
                label4.Visible = false;
            }
            else if (rbTron.Checked)
            {
                pQuetThang.Visible = false;
                pQuetVong.Visible = true;
                pXoanOc.Visible = false;
                txtNoSpiral.Visible = false;
                label4.Visible = false;
            }
            else if (rbXoanOc.Checked)
            {
                pQuetThang.Visible = false;
                pQuetVong.Visible = false;
                pXoanOc.Visible = true;
                txtNoSpiral.Visible = true;
                label4.Visible = true;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //may bay 1- dan su - mau do
            Coordinate toado1 = new Coordinate(1, 439, 451, 10, 1000, "abc");
            Coordinate toado2 = new Coordinate(2, 101, 377, 10, 1000, "abc");
            List<Coordinate> qd = new List<Coordinate>();
            qd.Add(toado1);
            qd.Add(toado2);
            FlyingItem mb = new FlyingItem("Dân sự", 1, qd, Color.Red);
            MainForm.listTarget.Add(mb);
            //may bay 2- dan su- da troi
            toado1 = new Coordinate(1, 239, 480, 8, 800, "abc");
            toado2 = new Coordinate(2, 440, 157, 8, 800, "abc");
            qd = new List<Coordinate>();
            qd.Add(toado1);
            qd.Add(toado2);
            mb = new FlyingItem("Dân sự", 2, qd, Color.Blue);
            MainForm.listTarget.Add(mb);
            //may bay 3 - quan su(ta) 
            toado1 = new Coordinate(1, 494, 115, 12, 2000, "abc");
            toado2 = new Coordinate(2, 352, 428, 12, 2000, "abc");
            qd = new List<Coordinate>();
            qd.Add(toado1);
            qd.Add(toado2);
            mb = new FlyingItem("Quân sự (ta)", 3, qd, Color.Yellow);
            MainForm.listTarget.Add(mb);
            //may bay 4
            toado1 = new Coordinate(1, 227, 79, 14, 2000, "abc");
            toado2 = new Coordinate(2, 436, 483, 14, 2000, "abc");
            qd = new List<Coordinate>();
            qd.Add(toado1);
            qd.Add(toado2);
            mb = new FlyingItem("Quân sự (địch)", 4, qd, Color.Purple);
            MainForm.listTarget.Add(mb);
            //may bay 5
            toado1 = new Coordinate(1, 58, 242, 6, 1200, "abc");
            toado2 = new Coordinate(2, 512, 372, 6, 1200, "abc");
            qd = new List<Coordinate>();
            qd.Add(toado1);
            qd.Add(toado2);
            mb = new FlyingItem("Quân sự (địch)", 5, qd, Color.Gray);
            MainForm.listTarget.Add(mb);
            //may bay 6
            toado1 = new Coordinate(1, 527, 197, 12, 1500, "abc");
            toado2 = new Coordinate(2, 390, 419, 12, 1500, "abc");
            qd = new List<Coordinate>();
            qd.Add(toado1);
            qd.Add(toado2);
            mb = new FlyingItem("Quân sự (ta)", 6, qd, Color.Pink);
            MainForm.listTarget.Add(mb);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtNoSpiral.Text == "")
                { }
                else
                {
                    _radar.numberOfSpirals = Convert.ToInt32(txtNoSpiral.Text);
                    _radar.CreateBaseImageSpiralSweep(pXoanOc.Width);
                    pXoanOc.Image = _radar.outputImageSpiralSweep;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void txtMaxAngle_LostFocus(object sender, EventArgs e)
        {
            try
            {
                double max_angle = Convert.ToDouble(txtMaxAngle.Text);
                if (max_angle <= 3 || max_angle > 60)
                {
                    MessageBox.Show("Góc nâng của radar không đúng");
                    return;
                }
                else
                    _radar.maxAngle = max_angle;
            }
            catch
            {
                MessageBox.Show("Góc nâng của radar không đúng");
                return;
            }

        }
    }
}
